package vote.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import vote.domain.PageParam;
import vote.domain.entity.UserVote;
import vote.domain.entity.VoteContent;
import vote.domain.vo.VoteContentResp;
import vote.mapper.VoteContentMapper;
import vote.service.VoteContentService;
import vote.service.VoteService;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description:
 * Author:han
 * Date: 2024-05-21
 */
@Service
public class VoteContentServiceImpl extends ServiceImpl<VoteContentMapper, VoteContent>implements VoteContentService {
    @Lazy
    @Resource
    private VoteService voteService;
    @Override
    public IPage<VoteContentResp> getVoteContent(Long userId, PageParam pageParam) {
        Page<VoteContent> page = new Page<>(pageParam.getPageNum().intValue(), pageParam.getPageSize().intValue());
        // 1. 查询当前用户 **今日** 的投票记录
        List<UserVote> userVotes = voteService.list(
                new LambdaQueryWrapper<UserVote>()
                        .eq(UserVote::getUserId, userId)
                        .eq(UserVote::getVoteDate, LocalDate.now())
        );
        Set<Long> votedItemIds = userVotes.stream().map(UserVote::getItemId).collect(Collectors.toSet());
        // 3. 查询投票内容
        LambdaQueryWrapper<VoteContent> queryWrapper = new LambdaQueryWrapper<>();
        Page<VoteContent> voteContentPage = this.page(page, queryWrapper);
        // 按 area 分组并在组内按投票数降序排序
        Map<String, List<VoteContent>> groupedContent = voteContentPage.getRecords().stream()
                .collect(Collectors.groupingBy(VoteContent::getArea));
        List<VoteContent> sortedContent = new ArrayList<>();
        for (List<VoteContent> group : groupedContent.values()) {
            group.sort(Comparator.comparingInt(VoteContent::getVoteNum).reversed());
            sortedContent.addAll(group);
        }
        // 4. 转换为响应对象，设置 voted 状态
        List<VoteContentResp> resp = new ArrayList<>();
        for (VoteContent voteContent : sortedContent) {
            VoteContentResp voteContentResp = new VoteContentResp();
            BeanUtils.copyProperties(voteContent, voteContentResp);
            voteContentResp.setVoted(votedItemIds.contains(voteContent.getId()));
            resp.add(voteContentResp);
        }
        // 5. 封装分页结果
        IPage<VoteContentResp> respPage = new Page<>();
        respPage.setCurrent(voteContentPage.getCurrent());
        respPage.setSize(voteContentPage.getSize());
        respPage.setTotal(voteContentPage.getTotal());
        respPage.setRecords(resp);
        return respPage;
    }

}
